
package system.base;

import framework.core.*;
import system.SelectManager;
import system.data.*;

/*
	Author    :Jackey
	CreateTime:2008-02-27 11:04:38
*/
public class TableSequence extends TableSequenceBase 
{

//----system generated start----// 	
public TableSequence() 
{
	
}

private static void delete(DBConnection con,String id) throws Exception
{
	Conditions conditions = new Conditions();

	conditions.add(ID,id);
	
	delete(con,TABLE_NAME,conditions);
}
public static TableSequence fetch()
{
	return new TableSequence();
}
public static TableSequence copy(TableSequence srcObj)
{
	TableSequence obj = fetch();
	obj.copyBase(srcObj);
	return obj;
}
public static TableSequence fetch(DBConnection con,String id) throws Exception
{
	Conditions conditions = new Conditions();

	conditions.add(ID,id);
	
	return fetch(con,conditions);
}
public static TableSequence fetch(DBConnection con,Conditions conditions) throws Exception
{
	DBResult result = null;
	try
	{
		result = DBEngine.doSelect(con,"select * from " + TABLE_NAME,conditions);
		java.sql.ResultSet rs = result.getRs();
		TableSequence obj = null;
		if(rs.next())
		{
			obj = fetch(rs);
		}
		return obj;
	}
	finally
	{
		if(result!=null) result.clear() ;
	}
}
public static TableSequence fetch(java.sql.ResultSet rs) throws Exception
{
	TableSequence obj = new TableSequence();
	obj.initObject(rs);
	return obj;
}
protected void getUnique(DBConnection con) throws Exception
{
		setId(DBEngine.getId(con));
}
//----system generated end----//



//----add by Jackey start----//
    //得到产品序列号
    public static String getProductSeqVal(DBConnection con,String warehouseCode) throws Exception
    {
        String mReturn = "";
        int intReturn = getDataTableSeqVal(con,warehouseCode,Product.TABLE_NAME);
        
        if (intReturn > 0)
        {
            mReturn = StringUtil.addZero(String.valueOf(intReturn), 10);
        }        
        
        return mReturn;
    }
    
    //得到表序列号
    public static int getDataTableSeqVal(DBConnection con,String warehouseCode,String datatableName) throws Exception
    {
        int mReturn = Value.NULL_INT;
        try{
            String strNowDate = new DateUtil().getDateString(); 
            Conditions cs  = new Conditions();
            cs.add(WAREHOUSE_CODE,warehouseCode);
            cs.add(DATATABLE_NAME,datatableName);
            //cs.add(CREATE_TIME,strNowDate);
            TableSequence ts = TableSequence.fetch(con,cs);
            if (ts == null)
            {
                ts = TableSequence.fetch();
                ts.setCurVal(0);
                ts.setCreateTime(strNowDate);
                ts.setWarehouseCode(warehouseCode);
                ts.setDatatableName(datatableName);
            }
            ts.setCurVal(ts.getCurVal()+1);            
            ts.setLastUpdateTime(new DateUtil().getFullString());
            ts.save(con);
            
            mReturn = ts.getCurVal();
        }
        catch(Exception e)
        {
            e.printStackTrace();  
        }
        
        return mReturn;
    }

//----add by Jackey end----//

}

